<?php

// +---------------------------------------------------
// | Name: 星空框架  栏目管理模型
// +---------------------------------------------------
// | Version: V5.0
// +---------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +---------------------------------------------------
// | Time: 2021-05-28 15:46
// +---------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 栏目管理模型
 */
class Cate extends BaseModel
{
    use SoftDelete;
    protected $name = 'cate';
    protected $pk = 'cate_id';
    protected $deleteTime = 'delete_time';

    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:47:44+080
     */
    public static function onBeforeWrite($data)
    {
        // 写入前 更新/新增操作均会执行
        $data->update_time = date('Y-m-d H:i:s');
        $data->linkurl_back = self::getBackLink($data['type']); //获取后台菜单链接
        // 获取父级族谱
        $data->parent_str = self::checkParent($data['parent_id'], 'parent_str', 'cate_id');
        if ('' == $data->sign && $data->cate_name) {
            $data->sign = self::checkSign(getPinyin($data->cate_name));
        }
        if (isset($data->cate_id)) {
            $data->linkurl = self::getLink($data['act'], $data['parent_id'], $data['linkurl'], $data->sign, $data->cate_id);
        } else {
            $data->linkurl = self::getLink($data['act'], $data['parent_id'], $data['linkurl'], $data->sign);
        }
        if ($data->order_id <= 0) {
            $data->order_id = self::max('order_id') + 1;
        }
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
    }

    /**
     * @xk 新增后操作
     *
     * @method onAfterInsert
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:50:46+080
     */
    public static function onAfterInsert($data)
    {
        self::where(['cate_id' => $data['parent_id']])->save(['type' => '1', 'linkurl_back' => '']);
        $artice = new Artice();
        /*
         * @xk 如果类型为单页类型
         *
         * @var [type]
         */
        if (3 == $data['type']) {
            $cate_id = self::where(['sign' => $data['sign']])->value('`cate_id`');
            $info_data['title'] = $data['cate_name'];
            $info_data['cate_id'] = $cate_id; //栏目id
            $info_data['cate_str'] = $data['parent_str'].$cate_id.',';
            $info_data['cate_type'] = $data['type'];
            $info_data['act'] = 'add';
            $artice->create($info_data);
        }
    }

    /**
     * @xk 更新后操作
     *
     * @method onAfterUpdate
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:51:59+080
     */
    public static function onAfterUpdate($data)
    {
        self::where(['cate_id' => $data['parent_id']])->save(['type' => '1', 'linkurl_back' => '']);
        $artice = new Artice();
        // 更新后：获取的更新数据，已经更新数据
        $artice_info = $artice->where(['cate_id' => $data['cate_id'], 'cate_type' => 3])->field('`cate_type`,`artice_id`')->find();
        if (3 == $data['type']) {
            if (!$artice_info) {
                // 之前不存在这种单页的数据
                $info_data['title'] = $data['cate_name'];
                $info_data['cate_id'] = $data['cate_id']; //栏目id
                $info_data['cate_str'] = $data['parent_str'].$data['cate_id'].',';
                $info_data['cate_type'] = $data['type'];
                $info_data['act'] = 'edit';
                $artice->destroy(function ($query) use ($data) {
                    $query->where(['cate_id' => $data->cate_id]);
                });
                $artice->save($info_data);
            }
        } else {
            if ($artice_info) {
                $artice->destroy(function ($query) use ($data) {
                    $query->where(['cate_id' => $data['cate_id']]);
                });
            }
        }
    }

    /**
     * @xk 写入后操作
     *
     * @method onAfterWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:53:18+080
     */
    public static function onAfterWrite($data)
    {
        self::updateParentStr($data['menu_id'], $data['parent_str'], 'parent_id', 'cate_id'); //写入后置操作 更新族谱
    }

    /**
     * @xk 删除后操作
     *
     * @method onAfterDelete
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:54:04+080
     */
    public static function onAfterDelete($data)
    {
        self::destoryChildren($data['cate_id'], 'cate_id');
    }

    /**
     * @xk 拼接后台链接
     *
     * @method getBackLink
     *
     * @param int $type [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:55:20+080
     */
    public static function getBackLink($type = 0)
    {
        switch ($type) {
        case '1':
          return '';
          break;
        case '2':
          return 'Artice/artice';
          break;
        case '3':
          return 'Info/info';
          break;
        case '4':
          return 'Video/video';
          break;
        case '5':
          return 'Download/download';
          break;
        case '6':
          return 'Gbook/gbook';
          break;
        case '7':
          return 'Pics/pics';
          break;
        default:
          return '';
          break;
      }
    }

    /**
     * @xk 拼接前端链接
     *
     * @method getLink
     *
     * @param string $act       [description]
     * @param [type] $parent_id [description]
     * @param [type] $linkurl   [description]
     * @param [type] $sign      [description]
     * @param int    $id        [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:55:55+080
     */
    public static function getLink($act = 'create', $parent_id=0, $linkurl='', $sign='', $id = 0)
    {
        switch ($act) {
          case 'create':
            if (0 != $parent_id) {
                $linkurl = $linkurl.'/'.$sign;
            }
            break;
          case 'edit':
            $old_info = self::where(['cate_id' => $id])->field('`parent_id`,`sign`')->find();
            if ($parent_id != $old_info['parent_id']) {
                if (0 != $parent_id) {
                    $linkurl = $linkurl.'/'.$sign;
                }
            } else {
                if ($sign != $old_info['sign']) {
                    // 如果标志更改了
                    if (0 != $parent_id) {
                        $links = explode('/', $linkurl); //如果标志变了 那么对应的跳转链接也需要改变
                        $links[count($links) - 1] = $sign;
                        $linkurl = implode('/', $links);
                    }
                }
            }
          break;
        }

        return $linkurl;
    }
    /**
     * @xk 获取栏目链接
     *
     * @method getCateUrl
     * @param  integer                 $parent_id                [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:57:38+080
     */
    public static function getCateUrl($parent_id = 0)
    {
        $linkurl = self::where(['cate_id' => $parent_id])->value('linkurl');

        return $linkurl ? $linkurl : '';
    }
    /**
     * @xk 获取栏目类型
     *
     * @method getCateType
     * @param  [type]                  $cate_id                  [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-28T15:58:22+080
     */
    public static function getCateType($cate_id)
    {
        $type = self::where(['cate_id' => $cate_id])->value('type');

        return $type ? $type : 0;
    }
}
